#include <iostream>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

class Solution 
{
public:
    vector<vector<int>> threeSum(vector<int>& nums) 
    {
        vector<vector<int>> result;
        sort(nums.begin(), nums.end());
        if(nums.size() < 3)
        {
            return result;
        }
        for(int i = 0; i < nums.size() - 2; i++)
        {
            if(i > 0 && nums[i] == nums[i-1])
            {
                continue;
            }
            int left = i + 1;
            int right = nums.size() - 1;
            while(left < right)
            {
                int sum = nums[i] + nums[left] + nums[right];
                if(sum < 0)
                {
                    left++;
                }
                else if(sum > 0)
                {
                    right--;
                }
                else
                {
                    result.push_back({nums[i], nums[left], nums[right]});
                    while(left < right && nums[left] == nums[left + 1])
                    {
                        left++;
                    }
                    while(left < right && nums[right] == nums[right -1])
                    {
                        right--;
                    }
                    left++;
                    right--;
                }
            }
        }
        return result;
    }
};

int main()
{
    Solution sol;

    // 测试用例1: 经典例子
    vector<int> nums1 = {-1, 0, 1, 2, -1, -4};
    cout << "测试用例1: nums = [-1,0,1,2,-1,-4]" << endl;
    vector<vector<int>> result1 = sol.threeSum(nums1);
    cout << "结果: ";
    for(auto& triplet : result1)
    {
        cout << "[";
        for(int i = 0; i < triplet.size(); i++)
        {
            cout << triplet[i];
            if(i < triplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例2: 空数组
    vector<int> nums2 = {};
    cout << "测试用例2: nums = []" << endl;
    vector<vector<int>> result2 = sol.threeSum(nums2);
    cout << "结果: ";
    for(auto& triplet : result2)
    {
        cout << "[";
        for(int i = 0; i < triplet.size(); i++)
        {
            cout << triplet[i];
            if(i < triplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例3: 只有三个元素
    vector<int> nums3 = {0, 1, 1};
    cout << "测试用例3: nums = [0,1,1]" << endl;
    vector<vector<int>> result3 = sol.threeSum(nums3);
    cout << "结果: ";
    for(auto& triplet : result3)
    {
        cout << "[";
        for(int i = 0; i < triplet.size(); i++)
        {
            cout << triplet[i];
            if(i < triplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例4: 全零
    vector<int> nums4 = {0, 0, 0, 0};
    cout << "测试用例4: nums = [0,0,0,0]" << endl;
    vector<vector<int>> result4 = sol.threeSum(nums4);
    cout << "结果: ";
    for(auto& triplet : result4)
    {
        cout << "[";
        for(int i = 0; i < triplet.size(); i++)
        {
            cout << triplet[i];
            if(i < triplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    // 测试用例5: 有重复元素
    vector<int> nums5 = {-2, 0, 1, 1, 2};
    cout << "测试用例5: nums = [-2,0,1,1,2]" << endl;
    vector<vector<int>> result5 = sol.threeSum(nums5);
    cout << "结果: ";
    for(auto& triplet : result5)
    {
        cout << "[";
        for(int i = 0; i < triplet.size(); i++)
        {
            cout << triplet[i];
            if(i < triplet.size() - 1) cout << ",";
        }
        cout << "] ";
    }
    cout << endl << endl;

    return 0;
}
